package ai4games_tests.tests;

import java.util.LinkedList;

import game2dai.Domain;
import game2dai.World;
import game2dai.entities.Vehicle;
import game2dai.entityshapes.Picture;
import game2dai.entityshapes.ps.ArrowPic;
import game2dai.entityshapes.ps.Hints;
import game2dai.maths.Vector2D;
import game2dai.steering.AutoPilot;
import game2dai.steering.SBF;
import game2dai.utils.StopWatch;
import processing.core.PApplet;

/**
 * Demonstration of Wander behaviour
 * 
 * @author Peter
 *
 */
public class GameTest_001 extends PApplet {

	World w;
	Domain wd;
	LinkedList<Vector2D> route = new LinkedList<Vector2D>();
	int maxRouteSize = 2000;
	Vehicle v;
	Picture view;
	AutoPilot sbWander;
	StopWatch sw = new StopWatch();

	public void setup(){
		size(600,600);
		w = new World(width, height);
		wd = new Domain(50, 50, width-50, height-50);

		v = new Vehicle(new Vector2D(200,200), // position
				10, // collision radius
				new Vector2D(10,10), // velocity
				40, // maximum speed
				new Vector2D(1,1), // heading
				1, // mass
				10,  // turning rate
				200 // max force
		); 
		view = new ArrowPic(this);		
		view.showHints(Hints.HINT_WANDER | Hints.HINT_COLLISION | Hints.HINT_HEADING | Hints.HINT_VELOCITY);
//		view.removeHints(Hints.HINT_COLLISION | Hints.HINT_VELOCITY);
//		view.addHints(Hints.HINT_VELOCITY);

		v.renderer(view);
		v.worldDomain(wd, SBF.REBOUND);
		sbWander = new AutoPilot();
		sbWander.wanderOn();
//		sbWander.setWanderDetails(10, 30, 30);
		
		sbWander.wanderFactors(120, 20, 30);
		v.AP(sbWander);
		// Add this mover to the world
		w.add(v);

		// Initialise the route
		route.addLast(v.pos().get());

		frameRate(60);

		// Initialise the stop watch
		sw.reset();
	}

	public void draw(){
		double deltaTime =sw.getElapsedTime();
		background(220,255,220);
		fill(225,255,255);
		noStroke();
		rect(50, 50, width-100, height-100);
		drawRoute();
		w.update(deltaTime);
		w.draw();
	}

	public void drawRoute(){
		Vector2D curr, p0, p1;
		curr = v.pos();
		p0 = route.getLast();
		if(!Vector2D.areEqual(curr, p0)){
			route.addLast(curr.get());
			while(route.size()> maxRouteSize)
				route.removeFirst();
		}
		stroke(0,64,0);
		strokeWeight(1);
		p0 = route.getFirst();
		for(int i = 1; i < route.size(); i++){
			p1 = route.get(i);
			if(p0.distanceSq(p1)< 10000)
				line((float)p0.x, (float)p0.y, (float)p1.x, (float)p1.y);
			p0 = p1;
		}

	}
}

